USE [kymedi2b2data]
GO

/****** Object:  UserDefinedFunction [dbo].[LAB_CONSTRAINT]    Script Date: 10/22/2012 3:09:33 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO



CREATE FUNCTION [dbo].[LAB_CONSTRAINT] (@CONSTRAIN_OPERATOR VARCHAR(10), @CONSTRAIN_VALUE VARCHAR(50), @CONSTRAIN_TYPE VARCHAR(10))
RETURNS VARCHAR(MAX)
AS
BEGIN

	declare @sql varchar(max)  = ""
	
	set @sql = case when (@CONSTRAIN_OPERATOR IS NULL and @CONSTRAIN_VALUE IS NULL and @CONSTRAIN_TYPE IS NULL)
			   THEN ""
			   when @CONSTRAIN_TYPE = "FLAG" 
			   then "VALUEFLAG_CD = '"+@CONSTRAIN_VALUE+ "'"
			   when @CONSTRAIN_TYPE = 'NUMBER'
			   then "NVAL_NUM " + 
					CASE when @CONSTRAIN_OPERATOR = 'GT' THEN ">"
						 when @CONSTRAIN_OPERATOR = 'LT' THEN "<"
						 when @CONSTRAIN_OPERATOR = 'EQ' THEN "="
						 when @CONSTRAIN_OPERATOR = 'LE' THEN "<="
						 when @CONSTRAIN_OPERATOR = 'GE' THEN ">="
						 when @CONSTRAIN_OPERATOR = 'BETWEEN' THEN 'BETWEEN' END 
				+@CONSTRAIN_VALUE 
			   end + char(13)
	RETURN @sql
END



GO


